import { ErrorCode } from "../config/ErrorCode";
import { GameConfig } from "../config/GameConfig";
import { ServerConfig } from "../config/ServerConfig";
import { UserDao } from "../dao/UserDao";
import { ResponseModel } from "./ResponseModel";

export class UserModel {

    public static State_Type = {
        FengJin: 0, //封禁，冻结
        Normal: 1, //正常账号
        Test: 2, //测试账号
    }

    public userid: number
    public account: string;
    public nick_name: string;
    public token: string;
    public sex: number;
    public headimg: string;
    public last_login_ip: string
    public state: number
    public create_time: number
    public last_login_time: number;

    public json2Model(data: object) {
        this.userid = data["userid"]
        this.account = data["account"]
        this.nick_name = data["userid"]
        this.token = data["token"]
        this.sex = data["sex"]
        this.headimg = data["headimg"]
        this.last_login_ip = data["last_login_ip"]
        this.state = data["state"]
        this.create_time = new Date(data["create_time"]).getTime()
        this.last_login_time = new Date(data["last_login_time"]).getTime()
    }

    public static db2Model(data: object) {
        let model: UserModel = new UserModel()
        model.json2Model(data)
        return model
    }

    public static async login(account: string, token: string) {
        let respModel: ResponseModel = new ResponseModel()
        let accountResult: object = await UserDao.get_user_data(account)
        if (accountResult) {
            if (ServerConfig.nowServerConfig.status == ServerConfig.Status_Type.Weihu) {
                if (accountResult["state"] != GameConfig.Status_Type.Test) {
                    respModel.code = ErrorCode.Code.Server_Weihu;
                    respModel.msg = ErrorCode.code2Msg.get(respModel.code)
                }
            }
            if (respModel.code == ErrorCode.Code.Suc) {
                if (token == accountResult["token"]) {
                    respModel.data = accountResult
                } else {
                    respModel.code = ErrorCode.Code.LoginError_token
                    respModel.msg = ErrorCode.code2Msg.get(respModel.code)
                }
            }
        } else {
            respModel.code = ErrorCode.Code.LoginError_NoUser
            respModel.msg = ErrorCode.code2Msg.get(respModel.code)
        }
        return respModel;
    }
}